﻿#ifndef __AVOID_BW_CONTROL_TEXT_T_HEAD_FILE_INCLUDED__
#define __AVOID_BW_CONTROL_TEXT_T_HEAD_FILE_INCLUDED__
#include <string>
#include <cstring>
#include <stdexcept>

namespace swui
{
	class ctlText
	{
	public:
		ctlText(const std::string& str) : _isWide(false), _string(str) {}
		ctlText(const std::wstring& str) : _isWide(true), _wideString(str) {}
		ctlText(const char* str) : _isWide(false), _string(str) {}
		ctlText(std::wstring::const_pointer str) : _isWide(true), _wideString(str) {}
		bool isWideChar() const { return _isWide; }
		std::string& getStdString() 
		{
			if(isWideChar()) throw std::runtime_error("非法访问 std::wstring::pointer 在 ctlText::getStdString() 中");
			return _string; 
		}
		std::wstring& getStdWideString() 
		{
			if(!isWideChar()) throw std::runtime_error("非法访问 char* 在 ctlText::getStdWideString() 中");
			return _wideString; 
		}
		const std::string& getStdString() const 
		{
			if(isWideChar()) throw std::runtime_error("非法访问 std::wstring::pointer 在 ctlText::getStdString() 中");
			return _string; 
		}
		const std::wstring& getStdWideString() const 
		{
			if(!isWideChar()) throw std::runtime_error("非法访问 char* 在 ctlText::getStdWideString() 中");
			return _wideString; 
		}
		const char* getString() const { return getStdString().c_str(); }
		std::wstring::const_pointer getWideString() const { return getStdWideString().c_str(); } 
	public:
		ctlText toString() const;
		ctlText toWideString() const;
	public:
		std::size_t length() const 
		{
			if(isWideChar()) return strlen(_string.c_str());
			return wcslen(_wideString.c_str());
		}
	private:
		bool _isWide;
		std::wstring _wideString;
		std::string _string;
	};
}

#endif